package com.example.yls.demoa;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.sug.SuggestionSearch;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG ="MainActivity" ;
    private MapView mMapView=null;
    private BaiduMap mBaiduMap;
    private UiSettings mUiSettings;
    private LatLng mlatLng;
    private AutoCompleteTextView autoctv;
    private SuggestionSearch suggestionSearch;
    private String key;
    private TextWatcher textWatcher;
    private RoutePlanSearch routePlanSearch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
//        //定位地图logo
//        mMapView.setLogoPosition(LogoPosition.logoPostionCenterTop);
//        mBaiduMap.setPadding(20,20,20,20);
//        //实例化UiSettings类对象
//        mUiSettings = mBaiduMap.getUiSettings();
//        //tongg设置enable为true 或 false 选择是否显示指南针
//        mUiSettings.setCompassEnabled(false);

        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,
                        "单机 纬度" + latLng.latitude +
                        " 经度" + latLng.longitude,
                Toast.LENGTH_SHORT).show();

            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                Toast.makeText(MainActivity.this,
                        "MapPoi单机" + mapPoi.getName() +
                                " 经度" + mapPoi.getPosition().toString(),
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        mBaiduMap.setOnMapDoubleClickListener(new BaiduMap.OnMapDoubleClickListener() {
            @Override
            public void onMapDoubleClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,
                        "双击 纬度" + latLng.latitude +
                                " 经度" + latLng.longitude,
                        Toast.LENGTH_SHORT).show();
            }
        });
        mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,
                        "长按 纬度" + latLng.latitude +
                                " 经度" + latLng.longitude,
                        Toast.LENGTH_SHORT).show();
                //绘制marker图标
                //drwaMarker(latLng);

                //绘制折线
                //drawPloyline(latLng);

                //绘制矩形
                //drawRect(latLng);

                //绘制弧线
                //drawArc(latLng);

                //绘制圆形
                //drwaCircle(latLng);

                //绘制文字
                //drawText(latLng);

                //绘制信息窗
                drawInfoWindow(latLng);
            }
        });
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MainActivity.this,
                        marker.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    //绘制信息窗
    private void drawInfoWindow(LatLng latLng) {
        //用来构造InfoWindow的Button
        Button button = new Button(getApplicationContext());
        button.setBackgroundResource(R.mipmap.ic_launcher);
        button.setText("InfoWindow");

        //构造InfoWindow
        // point 描述的位置点
        //-100 InfoWindow相对于point在y轴的偏移量
        InfoWindow mInfoWindow = new InfoWindow(button, latLng, -100);

        //使InfoWindow生效
        mBaiduMap.showInfoWindow(mInfoWindow);
    }

    //绘制文字
    private void drawText(LatLng latLng) {
        //文字覆盖物位置坐标
        LatLng llText =latLng;

        //构建TextOptions对象
        OverlayOptions mTextOptions = new TextOptions()
                .text("百度地图SDK") //文字内容
                .bgColor(0xAAFFFF00) //背景色
                .fontSize(24) //字号
                .fontColor(0xFFFF00FF) //文字颜色
                .rotate(-30) //旋转角度
                .position(llText);

//在地图上显示文字覆盖物
        Overlay mText = mBaiduMap.addOverlay(mTextOptions);
    }

    //绘制圆形
    private void drwaCircle(LatLng latLng) {
        //圆心位置
        LatLng center = latLng;
        //构造CircleOptions对象
        CircleOptions mCircleOptions = new CircleOptions().center(center)
                .radius(1400)
                .fillColor(0xAA0000FF) //填充颜色
                .stroke(new Stroke(5, 0xAA00ff00)); //边框宽和边框颜色

        //在地图上显示圆
        Overlay mCircle = mBaiduMap.addOverlay(mCircleOptions);
    }

    //绘制弧线
    private void drawArc(LatLng latLng) {
        LatLng p1 = latLng;
        LatLng p2 = new LatLng(p1.latitude-0.02,p1.longitude+0.02);
        LatLng p3 = new LatLng(p1.latitude,p1.longitude+0.04);
        //构造ArcOptions对象
        OverlayOptions mArcOptions = new ArcOptions()
                .color(Color.RED)
                .width(10)
                .points(p1, p2, p3);
        //在地图上显示弧线
        Overlay mArc = mBaiduMap.addOverlay(mArcOptions);
    }

    //绘制矩形
    private void drawRect(LatLng latLng) {
        LatLng p1 = latLng;
        LatLng p2 = new LatLng(p1.latitude-0.05,p1.longitude);
        LatLng p3 = new LatLng(p1.latitude-0.05,p1.longitude+0.05);
        LatLng p4 = new LatLng(p1.latitude,p1.longitude+0.05);
        List<LatLng> points = new ArrayList<LatLng>();
        points.add(p1);
        points.add(p2);
        points.add(p3);
        points.add(p4);
        points.add(p1);

        //设置折线的属性
        OverlayOptions mOverlayOptions = new PolylineOptions()
                .width(10)
                .color(0xAAFF0000)
                .points(points);
        //在地图上绘制折线
        mBaiduMap.addOverlay(mOverlayOptions);
    }

    //绘制折线
    private void drawPloyline(LatLng latLng) {
        //构建折线点坐标
        LatLng p1 = latLng;
        LatLng p2 = new LatLng(p1.latitude-0.05,p1.longitude+0.05);
        LatLng p3 = new LatLng(p1.latitude,p1.longitude+0.1);

        List<LatLng> points = new ArrayList<LatLng>();
        points.add(p1);
        points.add(p2);
        points.add(p3);

        //设置折线的属性
        OverlayOptions mOverlayOptions = new PolylineOptions()
                .width(10)
                .color(0xAAFF0000)
                .points(points);
        //在地图上绘制折线
        mBaiduMap.addOverlay(mOverlayOptions);
    }

    //绘制marker图标
    private void drwaMarker(LatLng latLng) {
        //定义Maker坐标点
                LatLng point = latLng;
                //构建Maker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.timg);
                //构建MarkerOption,用于在地图上添加Marker
                OverlayOptions options = new MarkerOptions()
                        .title("标题")
                        .position(point)
                        .icon(bitmap);
                //在地图上添加Marker,并显示
                mBaiduMap.addOverlay(options);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

}
